﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Text;
using System.Data;
using System.IO;


public partial class Evaluate_TaskEvaluatesDetachment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindYearList();
            BindMonthList();
            init();
        }
    }

    void init()
    {

        CreateTable();

    }
    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();
    }
    private void BindMonthList()
    {
        for (int i = 1; i <= 12; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "月";
            li.Value = i.ToString();
            MonthList.Items.Add(li);
        }
        MonthList.SelectedValue = DateTime.Now.Month.ToString();
    }
    private void CreateTable()
    {
        PlaceHolder.Controls.Clear();
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string DepartmentWhere = "departmentid like '" + departmentid.Substring(0, 4) + RadioButtonList1.SelectedValue + "%'";
        string UseridWhere = "userid like ''" + departmentid.Substring(0, 4) + RadioButtonList1.SelectedValue + "%''";


        DataSet ds = SplitPage.ExecuteSQL("Select departmentid,ShortName From Account_Departments where " + DepartmentWhere + "order by departmentid");
        StringBuilder sql = new StringBuilder();

        sql.AppendLine("DECLARE @SQL varchar(8000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN UserId = ''' + DepartmentId + ''' THEN Score END) AS D' + DepartmentId FROM Account_Departments WHERE " + DepartmentWhere + "");
        sql.AppendLine("SET @SQL = 'SELECT TaskId ' +@SQL + ' FROM TaskUsers WHERE (" + UseridWhere + ") and TaskId IN (SELECT TaskId FROM Tasks WHERE YEAR(EndDate) =" + YearList.SelectedValue + " AND MONTH(EndDate)=" + MonthList.SelectedValue + " AND CreateDepartment=''" + departmentid + "'') GROUP BY TaskId'");
        sql.AppendLine("EXEC(@SQL)");

        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;

        Table tb = new Table();

        tb.Caption = "<span>" + YearList.SelectedItem.Text + MonthList.SelectedItem.Text + "工作任务审核</span>";


        tb.Width = Unit.Percentage(100);
        tb.CellPadding = 4;
        tb.CellSpacing = 0;

        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        tr.CssClass = "grid_header";

        tc = new TableCell();
        tc.HorizontalAlign = HorizontalAlign.Left;

        tc.Text = "任务名称";
        tc.Attributes.Add("style", "border-color:#CEE7F7;background-image:none; border-collapse: collapse;white-space:nowrap;  border-width: 1px; border-style:solid;");
        tr.Cells.Add(tc);

        int col = ds.Tables[0].Rows.Count;

        for (int i = 0; i < col; i++)
        {
            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Right;

            tc.Style.Add("writing-mode", "tb-lr");
            tc.Style.Add("white-space", "normal");
            tc.Text = ds.Tables[0].Rows[i][1].ToString();
            tc.Attributes.Add("style", "border-color:#CEE7F7;background-image:none; border-collapse: collapse;white-space:nowrap;  border-width: 1px; border-style:solid;");
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);
        }

        int row = 1;
        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();
            tr.CssClass = "grid_item";
            tc = new TableCell();
            tc.BackColor = System.Drawing.Color.White;

            string title = Task.GetTask((int)drv[0]).Title.Length <= 16 ? row + "、" + Task.GetTask((int)drv[0]).Title : row + "、" + Task.GetTask((int)drv[0]).Title.Substring(0, 16);
            tc.Attributes.Add("style", "border-color:#CEE7F7;background-image:none; border-collapse: collapse;white-space:nowrap;  border-width: 1px; border-style:solid;");

            tc.Text = title;
            tr.Cells.Add(tc);

            for (int i = 1; i <= col; i++)
            {
                tc = new TableCell();
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                string score = "";
                if (drv[i] == DBNull.Value)
                {
                    tc.Text = "未下达";
                }
                else
                {
                    if ((decimal)drv[i] > 0)
                        score = ((decimal)drv[i]).ToString("0");
                    else
                        score = "0";
                    tc.Text = "<a href=TaskEvaluate.aspx?RootId=" + drv[0].ToString() + "&UserId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + score + "</a>";

                }
                tc.Attributes.Add("style", "border-color:#CEE7F7;background-image:none; border-collapse: collapse;white-space:nowrap;  border-width: 1px; border-style:solid;");
                tc.HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
            row++;
        }

        PlaceHolder.Controls.Add(tb);

    }
    protected void ButtonEvaluate_Click(object sender, EventArgs e)
    {
        init();
    }
    protected void lkbExport_Click(object sender, EventArgs e)
    {
        ExportData("application/ms-excel", DateTime.Now.ToString("yyMdHm") + ".xls");
    }

    private void ExportData(string FileType, string FileName)
    {

        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        pnl.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }


    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        init();
    }
}